package vn.com.demand.lux.preferences;

import java.sql.SQLException;
import java.util.StringTokenizer;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.preference.PreferenceStore;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import vn.com.demand.lux.Activator;

import com.microsoft.jdbcx.sqlserver.SQLServerDataSource;
import com.swtdesigner.ResourceManager;

public class DataBaseConnectionPreferences extends PreferencePage implements
		IWorkbenchPreferencePage {
	
	private Text databaseName;
	private Text password;
	private Text loginName;
	private Text port;
	private Combo server;
	private PreferenceStore preferenceStore;
	
	/**
	 * Create the preference page
	 */
	public DataBaseConnectionPreferences() {
		super();
		setTitle("Database Connection");
		setImageDescriptor(ResourceManager.getPluginImageDescriptor(Activator.getDefault(), "icons/actions/database.png"));
	}

	/**
	 * Create contents of the preference page
	 * @param parent
	 */
	@Override
	public Control createContents(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new FormLayout());
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
		container.setLayoutData(data);

		final Group databaseGroup = new Group(container, SWT.NONE);
		databaseGroup.setText("Database");
		final FormData fd_databaseGroup = new FormData();
		fd_databaseGroup.bottom = new FormAttachment(0, 160);
		fd_databaseGroup.right = new FormAttachment(100, -9);
		fd_databaseGroup.left = new FormAttachment(0, 0);
		fd_databaseGroup.top = new FormAttachment(0, 5);
		databaseGroup.setLayoutData(fd_databaseGroup);
		databaseGroup.setLayout(new FormLayout());
		
		final Label sqlServerLabel = new Label(databaseGroup, SWT.RIGHT);
		final FormData fd_sqlServerLabel = new FormData();
		fd_sqlServerLabel.bottom = new FormAttachment(0, 45);
		fd_sqlServerLabel.top = new FormAttachment(0, 25);
		sqlServerLabel.setLayoutData(fd_sqlServerLabel);
		sqlServerLabel.setText("SQL Server:");

		server = new Combo(databaseGroup, SWT.NONE);
		final FormData fd_server = new FormData();
		fd_server.left = new FormAttachment(0, 260);
		fd_server.right = new FormAttachment(100, -84);
		fd_server.top = new FormAttachment(sqlServerLabel, 0, SWT.TOP);
		String[] properties = Activator.getDefault().getDatabaseProperties();
		if (null == properties || properties.length <= 0) {
			properties = Activator.getDefault().getDefaultDatabaseProperties();
		}
		server.setText(properties[0]);
		server.setLayoutData(fd_server);

		Label txtPort;
		txtPort = new Label(databaseGroup, SWT.RIGHT);
		fd_sqlServerLabel.right = new FormAttachment(txtPort, 0, SWT.RIGHT);
		fd_sqlServerLabel.left = new FormAttachment(txtPort, 0, SWT.LEFT);
		final FormData fd_txtPort = new FormData();
		fd_txtPort.left = new FormAttachment(0, 165);
		txtPort.setLayoutData(fd_txtPort);
		txtPort.setText("Port:");

		port = new Text(databaseGroup, SWT.BORDER);
		fd_txtPort.bottom = new FormAttachment(port, 20, SWT.TOP);
		fd_txtPort.top = new FormAttachment(port, 0, SWT.TOP);
		final FormData fd_port = new FormData();
		fd_port.left = new FormAttachment(0, 260);
		fd_port.right = new FormAttachment(100, -148);
		fd_port.top = new FormAttachment(0, 65);
		port.setText(properties[1]);
		port.setLayoutData(fd_port);

		Group sqlServerAuthenticationGroup;
		sqlServerAuthenticationGroup = new Group(container, SWT.NONE);
		sqlServerAuthenticationGroup.setText("SQL Server Authentication");
		final FormData fd_sqlServerAuthenticationGroup = new FormData();
		fd_sqlServerAuthenticationGroup.bottom = new FormAttachment(100, -48);
		fd_sqlServerAuthenticationGroup.top = new FormAttachment(databaseGroup, 5, SWT.BOTTOM);
		fd_sqlServerAuthenticationGroup.right = new FormAttachment(100, -9);
		fd_sqlServerAuthenticationGroup.left = new FormAttachment(0, 5);
		sqlServerAuthenticationGroup.setLayoutData(fd_sqlServerAuthenticationGroup);
		sqlServerAuthenticationGroup.setLayout(new FormLayout());

		final Label loginLabel = new Label(sqlServerAuthenticationGroup, SWT.NONE);
		loginLabel.setAlignment(SWT.RIGHT);
		final FormData fd_loginLabel = new FormData();
		fd_loginLabel.right = new FormAttachment(0, 237);
		fd_loginLabel.bottom = new FormAttachment(0, 30);
		fd_loginLabel.top = new FormAttachment(0, 10);
		fd_loginLabel.left = new FormAttachment(0, 172);
		loginLabel.setLayoutData(fd_loginLabel);
		loginLabel.setText("Login name:");

		loginName = new Text(sqlServerAuthenticationGroup, SWT.BORDER);
		final FormData fd_loginName = new FormData();
		fd_loginName.left = new FormAttachment(0, 255);
		fd_loginName.right = new FormAttachment(100, -79);
		fd_loginName.bottom = new FormAttachment(0, 30);
		fd_loginName.top = new FormAttachment(0, 10);
		loginName.setText(properties[3]);
		loginName.setLayoutData(fd_loginName);

		final Label loginLabel_1 = new Label(sqlServerAuthenticationGroup, SWT.NONE);
		final FormData fd_loginLabel_1 = new FormData();
		fd_loginLabel_1.bottom = new FormAttachment(0, 75);
		fd_loginLabel_1.top = new FormAttachment(0, 55);
		fd_loginLabel_1.right = new FormAttachment(0, 237);
		fd_loginLabel_1.left = new FormAttachment(0, 170);
		loginLabel_1.setLayoutData(fd_loginLabel_1);
		loginLabel_1.setAlignment(SWT.RIGHT);
		loginLabel_1.setText("Password:");

		password = new Text(sqlServerAuthenticationGroup, SWT.BORDER | SWT.PASSWORD);
		final FormData fd_password = new FormData();
		fd_password.right = new FormAttachment(100, -79);
		fd_password.left = new FormAttachment(0, 255);
		fd_password.bottom = new FormAttachment(0, 75);
		fd_password.top = new FormAttachment(0, 55);
		password.setText(properties[4]);
		password.setLayoutData(fd_password);

		final Label label_1 = new Label(sqlServerAuthenticationGroup, SWT.NONE);
		label_1.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/policy-32x32x32b.png"));
		final FormData fd_label_1 = new FormData();
		fd_label_1.bottom = new FormAttachment(loginLabel, 40, SWT.TOP);
		fd_label_1.top = new FormAttachment(loginLabel, 0, SWT.TOP);
		fd_label_1.right = new FormAttachment(0, 80);
		fd_label_1.left = new FormAttachment(0, 45);
		label_1.setLayoutData(fd_label_1);

		Label databaseNameLabel;
		databaseNameLabel = new Label(databaseGroup, SWT.NONE);
		fd_txtPort.right = new FormAttachment(databaseNameLabel, 0, SWT.RIGHT);
		final FormData fd_databaseNameLabel = new FormData();
		fd_databaseNameLabel.left = new FormAttachment(0, 155);
		fd_databaseNameLabel.right = new FormAttachment(0, 245);
		fd_databaseNameLabel.bottom = new FormAttachment(0, 125);
		fd_databaseNameLabel.top = new FormAttachment(0, 105);
		databaseNameLabel.setLayoutData(fd_databaseNameLabel);
		databaseNameLabel.setAlignment(SWT.RIGHT);
		databaseNameLabel.setText("Database Name:");

		databaseName = new Text(databaseGroup, SWT.BORDER);
		final FormData fd_databaseName = new FormData();
		fd_databaseName.right = new FormAttachment(100, -84);
		fd_databaseName.bottom = new FormAttachment(0, 125);
		fd_databaseName.top = new FormAttachment(0, 105);
		fd_databaseName.left = new FormAttachment(0, 260);
		databaseName.setLayoutData(fd_databaseName);
		databaseName.setText(properties[2]);

		final Label label = new Label(databaseGroup, SWT.NONE);
		label.setImage(ResourceManager.getPluginImage(Activator.getDefault(), "icons/actions/database-48x48x32b.png"));
		final FormData fd_label = new FormData();
		fd_label.bottom = new FormAttachment(0, 98);
		fd_label.top = new FormAttachment(0, 38);
		fd_label.right = new FormAttachment(0, 90);
		fd_label.left = new FormAttachment(0, 40);
		label.setLayoutData(fd_label);

		
		
		//
		return container;
	}
	
	/**
	 * Initialize the preference page
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void performDefaults() {
		super.performDefaults();
		StringTokenizer token = new StringTokenizer(PreferenceConstants.DEFAULT_DATABASE_PROPERTIES, ";");
		int count = token.countTokens();
		String[] elements = new String[count];
		for (int i = 0; i < count; i++) {
			elements[i] = token.nextToken();
		}
		server.setText(elements[0]);
		port.setText(elements[1]);
		databaseName.setText(elements[2]);
		loginName.setText(elements[3]);
		password.setText(elements[4]);
	}

	@Override
	public boolean performOk() {
		String[] elements = new String[5];
		elements[0] = server.getText();
		elements[1] = port.getText();
		elements[2] = databaseName.getText();
		elements[3] = loginName.getText();
		elements[4] = password.getText();
		Activator.getDefault().setDatabasePropertiesPreferences(elements);
		return super.performOk();
	}

	@Override
	protected void contributeButtons(Composite parent) {
		super.contributeButtons(parent);
		Button button = new Button(parent, SWT.NONE);
		button.setText("Test Connection");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent arg0) {
				try {
					SQLServerDataSource ds = new SQLServerDataSource();
					ds.setDatabaseName(databaseName.getText());
					ds.setServerName(server.getText());
					ds.setUser(loginName.getText());
					ds.setPassword(password.getText());
					ds.setPortNumber(Integer.valueOf(port.getText()));
					ds.getConnection();
					MessageBox box = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_INFORMATION);
					box.setMessage("Connection succefull!");
					box.open();
				} catch (SQLException e) {
					MessageBox box = new MessageBox(Display.getCurrent().getActiveShell(), SWT.ICON_ERROR);
					box.setMessage("Connection failed, please check again");
					box.open();
					e.printStackTrace();
				}
			}
		});
		Button installButton = new Button(parent, SWT.NONE);
		installButton.setText("Install DB");
	}

	@Override
	protected void performApply() {
		super.performApply();
		String[] elements = new String[5];
		elements[0] = server.getText();
		elements[1] = port.getText();
		elements[2] = databaseName.getText();
		elements[3] = loginName.getText();
		elements[4] = password.getText();
		Activator.getDefault().setDatabasePropertiesPreferences(elements);
	}

}
